package com.longsunhd.yum.buffalo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.longsunhd.yum.buffalo.util.GlobalClient;
import com.longsunhd.yum.buffalo.util.ToastUtils;
import com.hks.power.HksPower;

import butterknife.ButterKnife;
import butterknife.OnClick;


public class EntryActivity extends AppCompatActivity {

    private ScreenStatusReceiver screenStatusReceiver;
    private boolean isClient = false;

//    static {
//        System.loadLibrary("native-lib");
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.entry_layout);
        ButterKnife.bind(this);
        initAndroidPorts();

        screenStatusReceiver = new ScreenStatusReceiver();
        IntentFilter screenStatusIF = new IntentFilter();
        screenStatusIF.addAction(Intent.ACTION_SCREEN_ON);
        screenStatusIF.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(screenStatusReceiver, screenStatusIF);
    }


    @OnClick(R.id.rfidConfig)
    public void rfidConfig() {
        if (isClient) {
            Intent intent = new Intent(this, RfidConfigActivity.class);
            startActivity(intent);
        } else {
            ToastUtils.showText("未连接");
        }
    }

    @OnClick(R.id.readOrWrite)
    public void readOrWrite() {
        if (isClient) {
            Intent intent = new Intent(this, ReadOrWriteActivity.class);
            intent.putExtra("isClient", isClient);
            startActivity(intent);
        } else {
            ToastUtils.showText("未连接");
        }
    }

    @OnClick(R.id.version)
    public void test() {
        if (isClient) {
            Intent intent = new Intent(this, TestActivity.class);
            startActivity(intent);
        } else {
            ToastUtils.showText("未连接");
        }
    }

    //初始化连接
    public void initAndroidPorts() {
        String hks = "/dev/ttysWK0:115200";
        HksPower.uhf_power(1);
        if (GlobalClient.getClient().openAndroidSerial(hks, 1000)) {
            isClient = true;
            ToastUtils.showText("连接成功");
        } else {
            isClient = false;
            ToastUtils.showText("连接失败");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isClient) {
            GlobalClient.getClient().close();
        }
        HksPower.uhf_power(0);
        unregisterReceiver(screenStatusReceiver);
    }

    static class ScreenStatusReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
                HksPower.uhf_power(1);
            } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
                HksPower.uhf_power(0);
            }
        }
    }

//
//    public native void uhf_power(int status);
//
//    public native void uhf_1io(int val);
}
